﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Collections;
using AppFx.Collections;
using AppFx.ComponentModel;
using AppFx.ComponentModel.Descriptive;

namespace WindowsFormsApplication2
{
	public partial class Form1 : Form
	{
		ObjectView<ComplexType> view;
		public Form1()
		{
			InitializeComponent();
			IList<ComplexType> lst = new List<ComplexType>();
			for (int i = 1; i <= 2000; i++)
			{
				lst.Add(new ComplexType() { ID = i, Type = new CustomType() { Title = "Herr", Name = "Achim", LastName = "Schnell" } });
			}
			for (int i = 2001; i <= 4000; i++)
			{
				lst.Add(new ComplexType() { ID = i, Type = new CustomType() { Title = "Frau", Name = "Katharina", LastName = "Diegel" } });
			}
			view = new ObjectView<ComplexType>(lst, new DeepCompiledTypeDescriptionProvider());
			view.AddIndex(new EvaluatedPropertyDesciptor(view, lst.First(), lst.First().GetType(), typeof(int), new Attribute[0], "SUM(Transfers)", "ID2"));
			dataGridView1.DataSource = view;
		}

		private void button2_Click(object sender, EventArgs e)
		{
			try
			{
				dataGridView1.DataSource = null;
				view.Filter = textBox1.Text;
			}
			catch (Exception ex)
			{
				textBox1.Text = "";
				MessageBox.Show(ex.Message, "Evaluator", MessageBoxButtons.OK, MessageBoxIcon.Error);
			}
			finally
			{
				dataGridView1.DataSource = view;
			}
		}
	}

	public class ComplexType
	{
		static Random rnd = new Random(5);
		[DisplayName("Identifikation")]
		public int ID { get; set; }

		public List<int> Transfers { get; set; }

		public CustomType Type { get; set; }

		public ComplexType()
		{
			Transfers = new List<int>();
			int amount = rnd.Next(1, 100);

			for (int i = 0; i < amount; i++)
				Transfers.Add(rnd.Next(1, 500));
		}
	}

	public class CustomType
	{
		[DisplayName("Titel")]
		public string Title { get; set; }

		[DisplayName("Name")]
		public string Name { get; set; }

		[DisplayName("Nachname")]
		public string LastName { get; set; }

		public override string ToString()
		{
			return String.Format("{0} {1} {2}", Title, Name, LastName);
		}
	}
}
